//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Providers.h"
#include "FormUtilities.h"
#include "ProviderDlg.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmProviders::TfrmProviders(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->dbMain = dbMain; 
   providers = new TStringList();
   providers->Duplicates = dupAccept;
   bridge = new TProviderBridge( dbMain);
}

void TfrmProviders::SetButtonsState( int row)
{
   bool enabled;

   enabled = ( providers->Count > 0);
   
   btnUpdate->Enabled = enabled;
   btnDelete->Enabled = enabled;
}

void TfrmProviders::RefreshGrid()
{
   TProvider* provider;
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetProviders( providers);

   if ( providers->Count > 1)
      rowCount = providers->Count + 1;

   grdProviders->RowCount = rowCount;

   for ( i = 1; i < grdProviders->RowCount; i++)
      grdProviders->Rows[ i]->Clear();

   for ( i = 0; i < providers->Count; i++)
   {
      provider = ( TProvider*)providers->Objects[ i];

      grdProviders->Cells[ 0][ i + 1] = provider->name;
      grdProviders->Cells[ 1][ i + 1] = provider->address;
      grdProviders->Cells[ 2][ i + 1] = provider->phone;
   }//for

   TFormUtilities::SetLastColumnWidth( grdProviders);

   SetButtonsState( grdProviders->Row);
}


//---------------------------------------------------------------------------

void __fastcall TfrmProviders::FormShow(TObject *Sender)
{
   grdProviders->Cells[ 0][ 0] = "NAME";
   grdProviders->Cells[ 1][ 0] = "ADDRESS";
   grdProviders->Cells[ 2][ 0] = "PHONE";

   RefreshGrid();
}
//---------------------------------------------------------------------------


void __fastcall TfrmProviders::btnAddClick(TObject *Sender)
{
   TfrmProviderDlg * frmProviderDlg;

   frmProviderDlg = new TfrmProviderDlg( this, bridge, NULL);

   try
   {
      if ( frmProviderDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmProviderDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmProviders::FormDestroy(TObject *Sender)
{
   int i;
   
   delete bridge;
   
   TStringUtilites :: Clear( providers);
   delete providers;
}
//---------------------------------------------------------------------------

void __fastcall TfrmProviders::btnDeleteClick(TObject *Sender)
{
   TProvider* provider;

   provider = ( TProvider*)providers->Objects[ grdProviders->Row - 1];

   if ( bridge->InUse( provider))
   {
      MessageDlg( "Provider in use. Unable to delete.",
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);
   }
   else if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
        bridge->Delete( provider->id);
        delete provider;
        providers->Delete( grdProviders->Row - 1);
        RefreshGrid();
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmProviders::grdProvidersSelectCell(TObject *Sender,
      int ACol, int ARow, bool &CanSelect)
{
   SetButtonsState( ARow);        
}
//---------------------------------------------------------------------------

void __fastcall TfrmProviders::btnUpdateClick(TObject *Sender)
{
   TfrmProviderDlg * frmProviderDlg;
   TProvider * provider;

   provider = ( TProvider*)providers->Objects[ grdProviders->Row - 1];
   frmProviderDlg = new TfrmProviderDlg( this, bridge, provider);

   try
   {
      if ( frmProviderDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmProviderDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmProviders::grdProvidersKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmProviders::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------



